package com.vincent.kdp.util;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.RFID.RFIDReader;
import com.RFID.TAGINFO;
import com.vincent.kdp.util.UhfDev;

import java.util.List;

/**
 * Created by fuxingxing on 17/1/23.
 */

public class InventoryApi {

    private String ip;
    private String port="4001";
    private int modType =2;
    private UhfDev uhfDev = UhfDev.getInstance();
    private boolean isInventory=false;
    private static InventoryApi instance = new InventoryApi();

    private Activity context;
    private Handler msgHandler;

    private InventoryApi(){

    }

    public static InventoryApi instance(Activity context){
        instance.context=context;
        return instance;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String connectReader() {
        if(uhfDev.IsOpen()){
          Log.d("UHF_DEV","社备已经开启");
        }else{
            if (!uhfDev.init(modType)) {
                String msg="初始化失败";
                return msg;
            }
            if (2 != modType) {
                if (!uhfDev.Open()) {
                    String msg="连接失败,请检查设置";
                    return msg;
                }
            } else {
                if (ip==null||"".equals(ip)) {
                    String msg="IP地址无效";
                    return msg;
                }
                int port = 4001;
                if (!uhfDev.OpenByTcp(ip, port)) {
                    String msg="连接失败:可能设备没有开启或者没有接入局域网";
                    return msg;
                }
            }
        }
        return "0000";
    }

    private void setScreenOn(boolean bOn) {
        if (bOn){
            //PowerMoniter.getInstance().acquireWakeLock(context);
        }else{
            //PowerMoniter.getInstance().releaseWakeLock();
        }
    }

    public void startInventory(){
        if (isInventory)
            return;
        isInventory = true;
        uhfDev.inventory();
    }

    public void stopInventory() {
        isInventory = false;

        uhfDev.pause();
        setScreenOn(false);

    }

    public void setHandleFuncAndCallback(Handler msgHandler ){
        this.msgHandler=msgHandler;
        setHandleFunc(msgHandler);
        setUhfDevCallback(msgHandler);
    }

    public void setHandleFunc(Handler msgHandler ){
        uhfDev.SetHandleFunc(msgHandler);
    }


    public RFIDReader.InvCallBack setUhfDevCallback(final Handler msgHandler){
        RFIDReader.InvCallBack mInvCallback = new RFIDReader.InvCallBack() {
            public String HexToString(byte[] bArray) {
                StringBuffer sb = new StringBuffer(bArray.length);
                String sTemp;
                for (int i = 0; i < bArray.length; i++) {
                    sTemp = Integer.toHexString(0xFF & bArray[i]);
                    if (sTemp.length() < 2)
                        sb.append(0);
                    sb.append(sTemp.toUpperCase());
                }
                return sb.toString();
            }

            private void sendMsg(String EpcID) {
                Message msg = new Message();
                msg.what = UhfDev.MSG_EPC;
                Bundle bundle = new Bundle();
                bundle.putString("msg", EpcID); // 往Bundle中存放数据
                msg.setData(bundle); // mes利用Bundle传递数据
                msgHandler.sendMessage(msg);// 用activity中的handler发送消息
            }

            int iCount = 0;

            @Override
            public void execute(List<TAGINFO> lstTagInfo) {
                iCount++;
                if (null == lstTagInfo || lstTagInfo.size() == 0)
                    Log.e("Inventory", "错误内容");
                // 处理数据
                String szTag = "";

                for (int i = 0; i < lstTagInfo.size(); i++) {
                    TAGINFO tfs = lstTagInfo.get(i);
                    if (null == tfs)
                        continue;
                    // 处理数据
                    szTag = HexToString(tfs.EpcId) + ";" + tfs.ReadCnt;
                    sendMsg(szTag);
                }
                Log.e("Inventory", "发送数据完毕");
            }
        };
        uhfDev.SetInvCallback(mInvCallback);
        return mInvCallback;
    }

}
